package io.lkt.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.lkt.common.util.Query;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.BUserPunchClockInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.lkt.modules.sys.entity.BPunchClockArticleInfoEntity;
import io.lkt.modules.sys.service.BPunchClockArticleInfoService;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;



/**
 * 打卡文章信息表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2021-01-19 17:19:13
 */
@RestController
@RequestMapping("/sys/bpunchclockarticleinfo")
public class BPunchClockArticleInfoController {
    @Autowired
    private BPunchClockArticleInfoService bPunchClockArticleInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:bpunchclockarticleinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        //PageUtils page = bPunchClockArticleInfoService.queryPage(params);

        Query query = new Query(params);
        List<BPunchClockArticleInfoEntity> list = bPunchClockArticleInfoService.queryList(query);
        int total = bPunchClockArticleInfoService.queryTotal();

        PageUtils page = new PageUtils(list,total,query.getLimit(), query.getPage());

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:bpunchclockarticleinfo:info")
    public R info(@PathVariable("id") Integer id){
        BPunchClockArticleInfoEntity bPunchClockArticleInfo = bPunchClockArticleInfoService.getById(id);

        return R.ok().put("bPunchClockArticleInfo", bPunchClockArticleInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:bpunchclockarticleinfo:save")
    public R save(@RequestBody BPunchClockArticleInfoEntity bPunchClockArticleInfo){
        bPunchClockArticleInfoService.save(bPunchClockArticleInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:bpunchclockarticleinfo:update")
    public R update(@RequestBody BPunchClockArticleInfoEntity bPunchClockArticleInfo){
        ValidatorUtils.validateEntity(bPunchClockArticleInfo);

        BPunchClockArticleInfoEntity info = new BPunchClockArticleInfoEntity();
        info.setId(bPunchClockArticleInfo.getId());
        info.setClockArticleTitle(bPunchClockArticleInfo.getClockArticleTitle());
        info.setClockArticleImgUrl(bPunchClockArticleInfo.getClockArticleImgUrl());
        info.setClockArticleThumbnailUrl(bPunchClockArticleInfo.getClockArticleThumbnailUrl());
        info.setClockArticleAudioUrl(bPunchClockArticleInfo.getClockArticleAudioUrl());
        info.setClockArticleContent(bPunchClockArticleInfo.getClockArticleContent());
        info.setModifyTime(new Date());

        bPunchClockArticleInfoService.updateById(info);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:bpunchclockarticleinfo:delete")
    public R delete(@RequestBody Integer[] ids){
        bPunchClockArticleInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/detail")
    public R detail(@RequestParam Map<String, Object> params) {
        if (params.get("id") == null || params.get("id").equals("")) {
            return R.error("参数不合法");
        }
        Query query = new Query(params);
        List<BUserPunchClockInfoEntity> list = bPunchClockArticleInfoService.getUserPunchClockInfo(query);
        int totalCount = bPunchClockArticleInfoService.getUserPunchClockTotal(params);
        PageUtils page = new PageUtils(list, totalCount, query.getLimit(), query.getPage());
        return R.ok().put("page", page);
    }

}
